package com.dingding.service.account.core.dao.impl;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Userintegral;
import com.dingding.service.account.core.dao.IUserIntegralDao;
import com.dingding.service.account.core.dao.IUserIntegralDetailDao;
import com.dingding.service.account.core.dao.sql.UserIntegralSQL;

/**
 * 
 * @ClassName: UserIntegralDaoImpl 
 * @Description: 用户积分表DAO
 * @author CHEHAUNBO 
 * @date 2016年3月30日 下午4:19:32
 * 
 */
@Component("userIntegralDao")
public class UserIntegralDaoImpl extends BaseDaoImpl<Userintegral> implements IUserIntegralDao {

	private Logger log = Logger.getLogger(UserIntegralDaoImpl.class);

	// 积分明细DAO
	@Resource(name = "userIntegralDetailDao")
	private IUserIntegralDetailDao userIntegralDetailDao;

	/**
	 * 
	 * @Title: addUserIntegral
	 * @Description: 添加用户积分
	 * @param userLoginId
	 *            用户登录主键ID
	 * @param intergal 
	 * 			     添加积分数量
	 * @return boolean true 成功 false 失败
	 * @author CHEHUANBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUserIntegral(String userLoginId, BigDecimal integral) {

		try {

			// 增加用户积分
			int row = super.updateSqlRow(UserIntegralSQL.ADD_USER_INTEGRAL, integral, userLoginId);

			if (row == 1) { // 添加成功
				return true;
			} else { // 添加失败
				return false;
			}

		} catch (Exception e) {
			log.error("添加用户积分出现异常！", e);
			return false;
		}

	}

	/**
	 * 
	 * @Title: deductUserIntegral
	 * @Description: 扣除用户积分
	 * @param userLoginId
	 *            用户登录主键ID
	 * @param intergal 
	 * 			     添加积分数量
	 * @return boolean true 成功 false 失败
	 * @author CHEHUANBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean deductUserIntegral(String userLoginId, BigDecimal integral) {

		try {

			// 增加用户积分
			int row = super.updateSqlRow(UserIntegralSQL.DEDUCT_USER_INTEGRAL, integral, userLoginId);
			if (row == 1) { // 扣除成功
				return true;
			} else { // 扣除失败
				return false;
			}

		} catch (Exception e) {
			log.error("扣除用户积分出现异常！", e);
			return false;
		}

	}

}
